import { create } from 'zustand'

const { ipcRenderer } = window.require('electron')

interface WindowState {
  opacity: number
  isClickThrough: boolean
  increaseOpacity: () => void
  decreaseOpacity: () => void
  toggleClickThrough: () => void
  handleWindowControl: (action: 'minimize' | 'maximize' | 'close') => void
}

export const useWindowStore = create<WindowState>((set, get) => ({
  opacity: 1,
  isClickThrough: false,

  increaseOpacity: async () => {
    const { opacity } = get()
    const newOpacity = Math.min(1, opacity + 0.1)
    try {
      const success = await ipcRenderer.invoke('set-window-opacity', newOpacity)
      if (success) {
        set({ opacity: newOpacity })
      }
    } catch (error) {
      console.error('Error setting window opacity:', error)
    }
  },

  decreaseOpacity: async () => {
    const { opacity } = get()
    const newOpacity = Math.max(0.1, opacity - 0.1)
    try {
      const success = await ipcRenderer.invoke('set-window-opacity', newOpacity)
      if (success) {
        set({ opacity: newOpacity })
      }
    } catch (error) {
      console.error('Error setting window opacity:', error)
    }
  },

  toggleClickThrough: async () => {
    const { isClickThrough } = get()
    const newState = !isClickThrough
    try {
      const success = await ipcRenderer.invoke('set-ignore-mouse-events', newState)
      if (success) {
        set({ isClickThrough: newState })
      }
    } catch (error) {
      console.error('Error setting ignore mouse events:', error)
    }
  },

  handleWindowControl: (action: 'minimize' | 'maximize' | 'close') => {
    ipcRenderer.send(`window-${action}`)
  }
})) 